//
//  BaseTableView.m
//  BasicProject
//
//  Created by 马军凯 on 2023/4/13.
//

#import "BaseTableView.h"

@interface BaseTableView ()
<UITableViewDataSource, UITableViewDelegate>

@end

@implementation BaseTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        [self createTableView];
    }
    return self;
}
#pragma mark 创建tableView
- (void)createTableView {
    self.delegate = self;
    self.dataSource = self;
    self.estimatedRowHeight = UITableViewAutomaticDimension;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
        self.sectionHeaderTopPadding = 0;
    }
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.backgroundColor = [UIColor whiteColor];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.separatorInset = UIEdgeInsetsZero;
    //尾
    self.tableFooterView = UIView.new;
}
#pragma mark UITableView dataSource
//几组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections ?: 1;
}
//每组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.rows) {
        NSInteger sectionNum = 1;
        if (self.sections) {
            sectionNum = self.sections;
        }
        if (sectionNum != self.rows.count) {
            NSLog(@"组和个数不一致");
            return 0;
        }
        NSNumber *num = self.rows[section];
        return [num integerValue];
    }
    return 0;
}
#pragma mark UITableView delegate
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.CreateCell(indexPath);
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.CellHeight) {
        return self.CellHeight(indexPath);
    }
    return UITableViewAutomaticDimension;
}
#pragma mark UITableView didSelect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.SelectedCell) {
        self.SelectedCell(indexPath);
    }
}
#pragma mark UITableView headView
//头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.CreateHeader) {
        return self.CreateHeader(section);
    }
    return nil;
}
//头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.HeaderHeight) {
        return self.HeaderHeight(section);
    }
    return CGFLOAT_MIN;
}
#pragma mark UITableView footView
//尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (self.CreateFooter) {
        return self.CreateFooter(section);
    }
    return nil;
}
//尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.FooterHeight) {
        return self.FooterHeight(section);
    }
    return CGFLOAT_MIN;
}
#pragma mark
//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.isAllowEdit;
}
//处理
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (self.DeleteCell) {
            self.DeleteCell(indexPath);
        }
    }
}
//删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
//
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
#pragma mark separator 分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.SeparEdge) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            cell.separatorInset = self.SeparEdge();
        }
        if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            cell.preservesSuperviewLayoutMargins = NO;
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            cell.layoutMargins = UIEdgeInsetsZero;
        }
    }
}


@end
